package com.hns.can.master.webservice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis.Constants;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;

import com.hns.tool.pub.UtilConfig;

/**
 * 
 *     
 * 项目名称：can    
 * 类名称：AbstractHenExport    
 * 类描述：    
 * 创建人：yecb    
 * 创建时间：2014年7月15日 下午2:26:44    
 * 修改人：yecb    
 * 修改时间：2014年7月15日 下午2:26:44    
 * 修改备注：    
 * @version   1.0
 *
 */
public class WebServiceClient {

	private static String ClientURL = UtilConfig.getString("webserver_url");

	/**
	 * 为了支持.NET方式新写的客户端调用方式
	 * 
	 * @param params key:参数名称;value:参数值
	 * @param methodname 方法名称
	 * @param serviceName 服务名称，根据服务名称获取调用地址
	 * @return Object[] 返回的数组数据
	 * @throws Exception
	 */
	public Object[] getNetWsClient(HashMap<String, Object> params,String methodname) throws Exception {

		ArrayList<String> paramnew = new ArrayList<String>();
		Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(new java.net.URL(ClientURL));
		// 增加命名空间申明 http://tempuri.org/
		call.setOperationName(new QName("http://tempuri.org/", methodname));
		Iterator iter = params.entrySet().iterator();
		while (iter.hasNext()) {
			Entry entry = (Entry) iter.next();
			paramnew.add(entry.getValue().toString());
			call.addParameter(new QName("http://tempuri.org/", entry.getKey()
					.toString()), Constants.XSD_STRING,
					javax.xml.rpc.ParameterMode.IN);
		}
		call.setReturnType(Constants.XSD_STRING);
		call.setUseSOAPAction(true);
		call.setSOAPActionURI("http://tempuri.org/" + methodname);
		return new Object[] { call.invoke(paramnew.toArray(new String[0])) };
	}

	public Object[] getCommWsClient(HashMap<String, Object> params,String methodname) throws Exception {
		ArrayList<String> paramnew = new ArrayList<String>();
		Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(new java.net.URL(ClientURL));
		call.setOperationName(new QName(methodname));
//		call.setTimeout(60);
		Iterator iter = params.entrySet().iterator();
		while (iter.hasNext()) {
			Entry entry = (Entry) iter.next();
			paramnew.add(entry.getValue().toString());
			call.addParameter(entry.getKey().toString(), Constants.XSD_STRING,
					javax.xml.rpc.ParameterMode.IN);
		}
		call.setReturnType(Constants.XSD_STRING);
		call.setUseSOAPAction(true);
		return new Object[] { call.invoke(paramnew.toArray(new String[0])) };
	}
	
	@SuppressWarnings("unchecked")
	public OMElement getDataSetByNetClient(Object param,String method,EndpointReference url) throws Exception{
		HashMap<String, Object> params = (HashMap<String, Object>) param;
		Options options = new Options();
		options.setAction("http://tempuri.org/" + method);// 调用接口方法
		options.setTo(url);
		options.setProperty(HTTPConstants.CHUNKED, "false");// 设置不受限制.
		options.setProperty(HTTPConstants.SO_TIMEOUT, 300000);
		ServiceClient sender = null;
		try {
			sender = new ServiceClient();
			sender.setOptions(options);
			OMFactory fac = OMAbstractFactory.getOMFactory();
			OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", "");
			OMElement methodOM = fac.createOMElement(method, omNs);
			Iterator iter = params.entrySet().iterator();
			while (iter.hasNext()) {
				Entry entry = (Entry) iter.next();
				OMElement name = fac.createOMElement(entry.getKey().toString(),omNs);// 设置入参名称
				name.setText(entry.getValue().toString());// 设置入参值
				methodOM.addChild(name);
			}
			methodOM.build();
			return sender.sendReceive(methodOM);
		} catch (AxisFault e) {
			throw new Exception("调用服务错误：" + e.getMessage(), e);
		}
	}
	
}
